'use strict';

angular.module('app.view-en.fan-data-problem')
    .config(['$stateProvider', function($stateProvider) {
        $stateProvider.state('en-statistics', {
            url: '/views-en/fan-data-problem/statistics',
            views: {
                'navigation': {
                    templateUrl: 'views-en/header/navigation.html',
                    controller: 'en-navigationCtrl'
                },
                'sidebar': {
                    templateUrl: 'views-en/sidebar/sidebar-fan-data-problem.html',
                    controller: 'en-sidebarCtrl'
                },
                'content': {
                    templateUrl: 'views-en/fan-data-problem/statistics/statistics.html',
                    controller: 'en-statisticsCtrl'
                }
            }
        })
    }])

    .controller('en-statisticsCtrl', ['$scope', 'Upload', 'communicationService', '$window',
        function($scope, Upload, communicationService, $window) {

            $scope.data = [{incomplete: [0, 1], inaccurate: [0, 1], inconsistent: [0, 1] }];
            var incompleteChart = echarts.init(document.getElementById("incomplete"));
            var inaccurateChart = echarts.init(document.getElementById("inaccurate"));
            var inconsistentChart = echarts.init(document.getElementById("inconsistent"));

            $window.onresize = function() {
                incompleteChart.resize();
                inaccurateChart.resize();
                inconsistentChart.resize();
            }

            var incompleteOption = {
                title : {
                    text: 'Missing Data Statistics',
                    x:'center'
                },
                tooltip : {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    right: 'right',
                    bottom:'bottom',
                    data: ['Missing Data','Normal Data']
                },
                series : [
                    {
                        name: 'Missing Imputation',
                        type: 'pie',
                        radius : '55%',
                        center: ['50%', '60%'],
                        data:[
                            {value:0, name:'Missing Data'},
                            {value:1, name:'Normal Data'},
                        ],
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };

            var inaccurateOption = {
                title : {
                    text: 'Abnormal Data Statistics',
                    x:'center'
                },
                tooltip : {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    right: 'right',
                    bottom:'bottom',
                    data: ['Abnormal Data','Normal Data']
                },
                series : [
                    {
                        name: 'Anomaly Detection and Repairing',
                        type: 'pie',
                        radius : '55%',
                        center: ['50%', '60%'],
                        data:[
                            {value:0, name:'Abnormal Data'},
                            {value:1, name:'Normal Data'},
                        ],
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };

            var inconsistentOption = {
                title : {
                    text: 'Inconsistent Attribute Statistics',
                    x:'center'
                },
                tooltip : {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    right: 'right',
                    bottom:'bottom',
                    data: ['Inconsistent Attribute','Normal Attribute']
                },
                series : [
                    {
                        name: 'Matching Inconsistent Attribute',
                        type: 'pie',
                        radius : '55%',
                        center: ['50%', '60%'],
                        data:[
                            {value:0, name:'Inconsistent Attribute'},
                            {value:1, name:'Normal Attribute'},
                        ],
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };

            function post() {
                communicationService.post('/api/fan-data-problem/statistics', {
                    operation: "query"
                }).then(function(data){
                    let temp = [];

                    // console.log("query");
                    // console.log(data);

                    temp.push({
                        incomplete: [data[0].key, data[0].key + data[0].value === 0? 1 :data[0].value],
                        inaccurate: [data[1].key, data[1].key + data[1].value === 0? 1 :data[1].value],
                        inconsistent: [data[2].key, data[2].key + data[2].value === 0? 1 : data[2].value],
                    });

                    $scope.data = temp[0];

                    // return true;
                }, function() {
                    // return false;
                })}

            post();

            let interval = setInterval(function() {
                let process = post();
                /*
                if(!process.$$state.value) {
                    console.log("cleanInterval");
                    clearInterval(interval);
                }
                */
            }, 10 * 1000);

            //
            $scope.$watch("data", function(newValue, oldValue){
               if(newValue === oldValue)
                   return;

                /*console.log("changed!");
                console.log(oldValue);
                console.log(newValue);
                */

                incompleteOption.series[0].data=[{value: $scope.data.incomplete[0], name:'Missing Data'}, {value:$scope.data.incomplete[1], name:'Normal Data'}];
                inaccurateOption.series[0].data=[{value: $scope.data.inaccurate[0], name:'Abnormal Data'}, {value:$scope.data.inaccurate[1], name:'Normal Data'}];
                inconsistentOption.series[0].data=[{value: $scope.data.inconsistent[0], name:'Inconsistent Attribute'}, {value:$scope.data.inconsistent[1], name:'Normal Attribute'}];

                incompleteChart.setOption(incompleteOption, true);
                inaccurateChart.setOption(inaccurateOption, true);
                inconsistentChart.setOption(inconsistentOption, true);
            }, true);


            // 清空值
            $scope.clear = function() {
                communicationService.post('/api/fan-data-problem/statistics', {
                    operation: "clear"
                }).then(function(data){
                    let temp = [];


                    // console.log("clear");
                    // console.log(data);

                    temp.push({
                        incomplete: [data[0].key, data[0].key + data[0].value === 0? 1 :data[0].value],
                        inaccurate: [data[1].key, data[1].key + data[1].value === 0? 1 :data[1].value],
                        inconsistent: [data[2].key, data[2].key + data[2].value === 0? 1 : data[2].value],
                    });


                    $scope.data = temp[0];

                }, function(err) {
                    console.warn(err);
                });
            };
    }]);

